package compro.admission.form.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import compro.admission.form.entity.User;
import compro.admission.form.service.UserService;

@Controller
public class ChangePasswordController {
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping("/showChangePassword")
	public String showLogin(Model model) {
		return "change_pwd";
	}
	@RequestMapping(value="/changePassword", method=RequestMethod.POST)
	public String changePassword(@RequestParam String old_pwd,@RequestParam String new_pwd,HttpSession session) {
		User user = (User)session.getAttribute("user");
		user.setPassword(new_pwd);
		userService.updateUser(user);
		return "redirect:/signout";
	}

	@RequestMapping(value="changePassword/validatePassword", method=RequestMethod.GET)
	public @ResponseBody String validatePassword(@RequestParam String old_pwd,HttpSession session) {
		User user = (User)session.getAttribute("user");
		if(user.getPassword().equals(old_pwd))
			return "true";
		else
			return "false";
	}
}
